// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: [],
    leaving_message: '',
    jsCode: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      total: options.amount,
      // seriesTitles: getApp().globalData.cartIds,
      cartNames: getApp().globalData.cartNames,
      seriesTitles: getApp().globalData.seriesTitles,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data
    getApp().globalData.addressId
    this.addressId()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  addressSelect: function () {
    wx.navigateTo({
      url: '/pages/user/addressselect/addressselect'
    })
  },
  addressId:function(){
    var that = this;
    // var index = e.currentTarget.dataset.index
    // var id = this.data.address[index].id
    wx.request({
      url: getApp().globalData.domain + 'api/order/dizhi?id=' + getApp().globalData.addressId,
      data: {
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(res)
        that.setData({
          address: res.data[0],
        })
      },
      fail: function (err) { },
      complete: function () { }
    });
  },
  leavingMessage:function(e){
    // console.log(e.detail.value)
    this.setData({
      leaving_message: e.detail.value
    })
  },
  pay: function () {
    var that=this
    var commodity_id = getApp().globalData.cartIds
    var commodity_name = getApp().globalData.cartNames
    var commodity_series_name = getApp().globalData.seriesTitles
    var total_price = that.data.total
    var receiving_address = that.data.address.id
    var detailed_address = that.data.address.detailed_address
    var name = that.data.address.name
    var phone_number = that.data.address.phone_number
    var leaving_message = that.data.leaving_message
    wx.login({
      success: function (res) {
        // console.log(res)
        that.setData({ jsCode: res.code })
        wx.request({
          url: getApp().globalData.domain + 'api/order/place_many_order?commodity_id=' + commodity_id + '&commodity_name=' + commodity_name + '&commodity_series_name=' + commodity_series_name + '&total_price=' + total_price + '&receiving_address=' + receiving_address + '&detailed_address=' + detailed_address + '&name=' + name + '&phone_number=' + phone_number + '&leaving_message=' + leaving_message + '&openid=' + getApp().globalData.openid + '&jsCode=' + that.data.jsCode,
          data: {
          },
          header: {
            'Content-Type': 'application/json'
          },
          method: "POST",
          success: function (res) {
            console.log(res)
            getApp().globalData.payStatus = res.data.package
            wx.requestPayment({
              'timeStamp': res.data.timeStamp.toString(),
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': res.data.signType,
              'paySign': res.data.paySign,
              'success': function (res) {
                console.log('付款成功')
                wx.request({
                  url: getApp().globalData.domain + 'api/order/status?package=' + getApp().globalData.payStatus,
                  data: {},
                  header: {
                    "Content-Type": "applciation/json"
                  },
                  method: "POST",
                  success: function (res) {
                    console.log(res);
                  },
                  fail: function (err) { },
                  complete: function () { }
                })
                console.log(res)
              },
              'fail': function (res) {
                console.log('付款失败')
                console.log(res)
              }
            })
          },
          fail: function (err) { },
          complete: function () { }
        })
      }
    })
  },
})